<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>p41购物车</title>
  <style>
    table {
      border: 1px solid #e9e9e9;
      border-collapse: collapse;
      border-spacing: 0;
    }

    th, td {
      padding: 8px 16px;
      border: 1px solid #e9e9e9;
      text-align: center;
    }

    th {
      background-color: antiquewhite;
      color: cadetblue;
      font-weight: 600;
    }
  </style>
</head>
<body>
  <div id="app">
    <div v-if="books.length">
      <table>
        <thead>
          <tr>
            <th></th>
            <th>书籍名称</th>
            <th>出版日期</th>
            <th>价格</th>
            <th>购买数量</th>
            <th>操作</th>
          </tr>
        </thead>

        <tbody>
          <tr v-for="(item, index) in books" :key="index">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.date}}</td>
            <td>{{item.price | showPrice}}</td>
            <td>
              <button @click="decrement(index)" 
                      v-bind:disabled="item.count <= 1">
                      -
              </button>
              {{item.count}}
              <button @click="increment(index)">+</button>
            </td>
            <td @click="removeThisline(index)">移除</td>
          </tr>
        </tbody>
      </table>
       <h2>总价格：{{totlePrice | showPrice}}</h2>
    </div>
    <div v-else>购物车为空</div>
  </div>

  <script src="../js/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        books: [
          {
            id: 1,
            name: '《算法导论》',
            date: '2006-9',
            price:85,
            count:1
          },
          {
            id: 2,
            name: '《UNIX编程艺术》',
            date: '2006-2',
            price:59,
            count:1
          },
          {
            id: 3,
            name: '《编程珠玑》',
            date: '2008-9',
            price:39.00,
            count:1
          },
          {
            id: 4,
            name: '《代码大全》',
            date: '2019-9',
            price:128,
            count:1
          },
        ],   //tbody中的数据
      },
      methods: {
         decrement(index) {   //实际是更改当前的count
          console.log('decrement',index)
          this.books[index].count--
         },
         increment(index) {
          console.log('increment',index)
          this.books[index].count++
         },
         removeThisline(index) {
          this.books.splice(index,1)
         }
      },
      computed: {   //计算属性本质上是一个属性,虽然看起来像是一个函数
        totlePrice() {
          let totlePrice = 0;
          for(let i = 0; i < this.books.length; i++){
            totlePrice += this.books[i].price * this.books[i].count
          }
          return totlePrice
        }
      },

      filters: {
        showPrice(price) {  //设置过滤器，vue自动获取item.price作为参数传入
          return '￥' + price.toFixed(2)
        }
      }
    })
  </script>
</body>
</html>